import {defineStore} from "pinia"
import type { Category } from "./interface";
import { getCategory1ListApi, getCategory2ListApi, getCategory3ListApi } from "@/api/product/category";

export const useCategoryStore = defineStore("category", {
    state: (): Category => {
      return {
        category1Id: undefined, // 初始化为undefined，为了一上来select显示请选择xxx文字
        category2Id: undefined,
        category3Id: undefined,
        category1List: [],
        category2List: [],
        category3List: [],
      };
    },
    getters: {},
    actions: {
      // 获取一级分类
      async getCategory1List() {
        this.category1List = await getCategory1ListApi();
      },
      // 获取二级分类
      async getCategory2List(category1Id: number) {
        this.category2List = await getCategory2ListApi(category1Id);
        this.category1Id = category1Id;
        // 让二级/三级分类不选中
        this.category2Id = undefined;
        this.category3Id = undefined;
        // 清空三级分类列表
        this.category3List = [];
      },
      // 获取三级分类
      async getCategory3List(category2Id: number) {
        this.category3List = await getCategory3ListApi(category2Id);
        this.category2Id = category2Id;
        this.category3Id = undefined;
      },
    },
  });
  